﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace JacketEmblem.Crane.UnitTests.NUnitExtensions
{
    public class TestDataFileJsonConverter<T> : JsonConverter where T : DataFileTestInfo
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(DataFileTestInfo[]);
        }

        public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
        {
            List<DataFileTestInfo> infoList = new List<DataFileTestInfo>();
            while (reader.TokenType != JsonToken.StartArray)
            {
                reader.Read();
            }

            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
            {
                T info = NewMethod(reader, serializer);
                infoList.Add(info);
            }

            while (reader.Read())
            {
            }

            return infoList.ToArray();
        }

        private static T NewMethod(JsonReader reader, JsonSerializer serializer)
        {
            T info = serializer.Deserialize<T>(reader);
            return info;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}